<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- card 卡片布局 -->
      <el-card class="box-card">
        <!-- tab 标签页布局 -->
        <el-tabs v-model="activeName">
          <el-tab-pane label="组织架构" name="first" class="tab-pane">
            <!-- layout布局 -->
            <el-row class="department-header" type="flex" justify="space-between" align="middle">
              <el-col :span="20">
                <i class="el-icon-s-home" />
                <span class="company">江苏传智播客教育科技股份有限公司</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <el-col>负责人</el-col>
                  <el-col>
                    <el-dropdown>
                      <span class="el-dropdown-link">
                        操作<i class="el-icon-arrow-down el-icon--right" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addDept">添加子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <el-tree :data="treeData" class="departments-tree" default-expand-all :props="defaultProps">
              <template v-slot="{data}">
                <el-row style="width:100%" type="flex" justify="space-between" align="middle">
                  <el-col :span="20">
                    <span>{{ data.name }}</span>
                  </el-col>
                  <el-col :span="4">
                    <el-row type="flex" justify="end">
                      <el-col>{{ data.manager }}</el-col>
                      <el-col>
                        <el-dropdown>
                          <span class="el-dropdown-link">
                            操作<i class="el-icon-arrow-down el-icon--right" />
                          </span>
                          <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item @click.native="addDept(data.id)">添加子部门</el-dropdown-item>
                            <el-dropdown-item @click.native="editDept(data.id)">编辑部门</el-dropdown-item>
                            <el-dropdown-item v-if="data && !data.children" @click.native="delDept(data.id)">删除部门</el-dropdown-item>
                          </el-dropdown-menu>
                        </el-dropdown>
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </template>
            </el-tree>
          </el-tab-pane>
        </el-tabs>
        <el-dialog
          :title="isEdit ? '编辑组织架构' : '新增组织架构'"
          :visible.sync="showDialog"
          width="50%"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
        >
          <deptDialog
            v-if="showDialog"
            :parent-id="parentId"
            :is-edit="isEdit"
            :origin-list="originList"
            @update-list="updateList"
            @close="closeConfirm"
          />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
// 引入子组件
import deptDialog from './deptDialog.vue'
export default {
  // 注册子组件
  components: {
    deptDialog
  },
  data() {
    return {
      activeName: 'first', // 被激活的 Tab 标签页
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDialog: false, // 控制弹框的显示和隐藏
      parentId: '',
      isEdit: false,
      originList: []
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {

    // 刷新列表，隐藏弹框
    updateList() {
      this.showDialog = false
      this.getDepartments()
    },

    // 点击取消按钮，弹窗关闭
    closeConfirm() {
      this.showDialog = false
    },

    // 组织架构数据的获取方法
    async getDepartments() {
      const res = await getDepartments()
      // console.log(res)
      if (!res.success) return this.$message.error(res.message)
      this.treeData = tranListToTreeData(res.data.depts)
      // 精简数组
      this.originList = res.data.depts.map(item => ({
        name: item.name,
        id: item.id,
        code: item.code,
        pid: item.pid
      }))
      // console.log(this.originList)
    },

    //
    handleNodeClick(data) {
      // console.log(data)
    },

    // 显示添加子部门
    addDept(id) {
      this.parentId = id
      this.showDialog = true

      // 点击添加edit为false
      this.isEdit = false
    },

    // 编辑部门
    editDept(id) {
      this.parentId = id
      this.showDialog = true

      // 点击编辑edit为true
      this.isEdit = true
    },

    // 删除部门
    async delDept(id) {
      const confirmRes = await this.$confirm('此操作将删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)

      // 如果点击取消,给用户提示
      if (confirmRes === 'cancel') return this.$message('取消删除')

      // 点击确定,调用API
      const res = await delDepartment(id)

      // 删除成功以后,给用户提示,渲染列表
      this.$message.success(res.message)
      this.getDepartments()
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px 80px;
.departments-tree{
  margin: 12px 0px;
}
  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }

  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }
}</style>
